package com.spring.sunflower.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.spring.sunflower.R;
import com.spring.sunflower.common.WalletActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.HelpDialog;
import com.spring.sunflower.dialog.NoticeDialog;
import com.spring.sunflower.dialog.OnPermissionIntroClickListener;
import com.spring.sunflower.dialog.TipsIntroDialog;
import com.spring.sunflower.util.DoubleClickUtils;
import com.spring.sunflower.util.LogUtils;
import com.spring.sunflower.util.NetUtils;


/**
 * Created by <a href="http://blog.csdn.net/student9128">Kevin</a> for Project Mellow on 2021/5/28.
 * <h3>Description:</h3>
 * <div>
 * Base class for all the fragments in the app extends {@link androidx.fragment.app.Fragment}.
 * <br/>There are some global methods in this class which maybe used in all the sub fragments.
 * </div>
 */


public class AppBaseFragment extends Fragment {
    //传递过来的参数Bundle，供子类使用
    protected Bundle args;

    /**
     * 创建fragment的静态方法，方便传递参数
     *
     * @param args 传递的参数
     * @return
     */
    public static <T extends Fragment> T newInstance(Class clazz, Bundle args) {
        T mFragment = null;
        try {
            mFragment = (T) clazz.newInstance();
        } catch (java.lang.InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        mFragment.setArguments(args);
        return mFragment;
    }

    /**
     * 初始创建Fragment对象时调用
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        args = getArguments();
    }

    /**
     * Tag,the class name.
     */
    public String TAG = getClass().getSimpleName();
    /**
     * can be prevent nullPointer.
     */
    protected FragmentActivity mActivity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = getActivity();
    }

    /**
     * check network is available.
     *
     * @return true or false.
     */
    public boolean networkAvailable() {
        return NetUtils.isNetworkAvailable(BaseApplication.getContext());
    }

    public void showNeedCharge() {
        NoticeDialog dialog = new NoticeDialog(mActivity, new OnPermissionIntroClickListener() {
            @Override
            public void onClickCancel() {
            }

            @Override
            public void onClickOK() {
                Intent intent = new Intent(mActivity, WalletActivity.class);
                startActivity(intent);
            }
        });
        dialog.setOnCreateViewListener(new NoticeDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                TextView tvTitle = dialog.findViewById(R.id.tvTitle);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvTitle.setText("余额提醒");
                tvDesc.setText("当前余额不足，无法进行该操作");
                tvDesc.setTextColor(getColorX(R.color.red_2));
                TextView tvOK = dialog.findViewById(R.id.tvOK);
                tvOK.setText("去充值");
            }
        });
        new XPopup.Builder(mActivity)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
    }

    /**
     * can be used for setting text color or background
     *
     * @param color
     * @return
     */
    public int getMyColor(int color) {
        return ContextCompat.getColor(mActivity, color);
    }

    /**
     * store value in SharePreferences.
     *
     * @param key key
     * @param str value
     */
    public void setSp(String key, String str) {
        SPUtils.getInstance().put(key, str);
    }

    public void setSp(String key, boolean b) {
        SPUtils.getInstance().put(key, b);
    }

    /**
     * get SharePreferences value.
     *
     * @param key key
     * @return value
     */
    public String getStringSp(String key) {
        return SPUtils.getInstance().getString(key);
    }

    public Boolean getBooleanSp(String key) {
        return SPUtils.getInstance().getBoolean(key);
    }

//===============Tools=================//

    public void showToast(String message) {
        ToastUtils.showShort(message);
    }

    public void showError(int resId) {
        showToast(getString(resId));
    }

    public void showLongToast(String message) {
        ToastUtils.showLong(message);
    }

    public void printD(String msg) {
        LogUtils.INSTANCE.logD(TAG + "=>", msg);
    }

    public void printI(String msg) {
        LogUtils.INSTANCE.logI(TAG + "=>", msg);
    }

    public void printE(String msg) {
        LogUtils.INSTANCE.logE(TAG + "=>", msg);
    }

    public void printW(String msg) {
        LogUtils.INSTANCE.logW(TAG + "=>", msg);
    }

    public void printV(String str) {
        LogUtils.INSTANCE.logV(TAG, str);
    }


    public void saveField(String key, String value) {
        SPUtils.getInstance().put(key, value);
    }
//    public void

    public void saveField(String key, Boolean value) {
        SPUtils.getInstance().put(key, value);
    }

    public void saveField(String key, int value) {
        SPUtils.getInstance().put(key, value);
    }

    public String getField(String key, String defaultValue) {
        return SPUtils.getInstance().getString(key, defaultValue);
    }

    public Boolean getField(String key, Boolean defaultValue) {
        return SPUtils.getInstance().getBoolean(key, defaultValue);
    }

    public int getField(String key, int defaultValue) {
        return SPUtils.getInstance().getInt(key, defaultValue);
    }

    public boolean isFastClick() {
        return DoubleClickUtils.isFastClick();
    }

    public String format(String x) {
        return String.valueOf(Double.valueOf(x).intValue());
    }

    public int getColorX(int colorId) {
        return ContextCompat.getColor(mActivity, colorId);
    }

    public void clearStorageUserData() {
        saveField(Constant.KEY_TOKEN, "");
        saveField(Constant.KEY_GENDER, "");
        saveField("birthday", "");
        saveField(Constant.KEY_SIGNATURE, "");
        saveField(Constant.KEY_NICKNAME, "");
        saveField(Constant.KEY_TYPE, "");
        saveField(Constant.KEY_USERID, "");
        saveField(Constant.KEY_PHOTO, "");
        saveField(Constant.KEY_MOBILE, "");
        saveField(Constant.KEY_WEALTH, "");
        saveField(Constant.KEY_USERID_CHATTER, "");
        saveField(Constant.KEY_AVATAR_CHATTER, "");
        saveField(Constant.KEY_NICKNAME_CHATTER, "");
        saveField(Constant.KEY_CHATTER_PRICE, "");
        saveField(Constant.CHATTED_TIME_STORAGE, "");
        saveField(Constant.KEY_APPLY_WITHDRAW, "");
    }

    public HelpDialog showHelpDialog(String title, String desc, @Nullable OnPermissionIntroClickListener listener) {
        HelpDialog dialog = new HelpDialog(mActivity, listener);
        dialog.setOnCreateViewListener(new HelpDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tv = dialog.findViewById(R.id.tv);
                tv.setText(title);
                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
                tvDesc.setText(desc);
            }
        });
        new XPopup.Builder(mActivity)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
        return dialog;
    }

    public TipsIntroDialog showTipDialog(@Nullable OnPermissionIntroClickListener listener) {
        TipsIntroDialog dialog = new TipsIntroDialog(mActivity, listener);
//        dialog.setOnCreateViewListener(new TipsIntroDialog.OnCreateViewListener() {
//            @Override
//            public void onCreateView() {
//                TextView tv = dialog.findViewById(R.id.tv);
//                tv.setText("温馨提示");
//                TextView tvDesc = dialog.findViewById(R.id.tvDesc);
//                tvDesc.setText(AppUtils.formatString(getString(R.string.video_tip), Color.parseColor("#2196f3"), "禁止涉黄", "封号处理"));
//            }
//        });
        new XPopup.Builder(mActivity)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
        return dialog;
    }

}